#include "MyhashTable.h"
#include "hashtable.h"
#include <fstream>

int main() {
    int m,n,x;  char c;
    MyHashTable<Student_info> H(m);
    string FilePath="F:\\temp\\Student_info.txt";
    ifstream in;
    ofstream out;
    cout<<"\n Succeed!";
    do{
        cout<<'\n';
        cout<<"\n I...Load";
        cout<<"\n X...Quit";
        cout<<"\n A...Add";
        cout<<"\n D...Delete";
        cout<<"\n F...Find";
        cout<<"\n S...Save";
        cout<<"\n P...Print \n";
        cin>>c;
        switch(c){
            case 'I':
            case 'i':
                in.open(FilePath);
                if(!in.is_open())cout<<"error in opening!";
                if(in.good())in>>m;
                if(in.good())in>>n;
                while(in.good()&&n>0){
                    Student_info res;
                    in>>res.key_num>>res.name>>res.sex>>res.home;
                    cout<<res.key_num<<res.name<<res.sex<<res.home<<endl;
                    switch(H.Insert(res,res.key_num)){
                        case Duplicate :cout<<"Reload";break;
                        case Overflow  :cout<<"Overflow";break;
                        case Success   :n--;
                    }
                }
                in.close();
                break;
            case 'S':
            case 's':
                out.open(FilePath);
                out<<m<<' '<<m;
                for (int i = 0; i < m; ++i) {
                    if(H.ht[i].key_!=-1){
                        out<<endl;
                        out<<H.ht[i].stu.key_num<<' '<<H.ht[i].stu.name<<' '
                        <<H.ht[i].stu.sex<<' '<<H.ht[i].stu.home;
                    }
                }
                out.close();
                cout<<"succeed!"<<endl;
                break;
            case 'A':
            case 'a':
                cout<<"Please input the varible:";
                Student_info res;
                cout<<"Num"<<endl;cin>>res.key_num;
                cout<<"Name"<<endl;cin>>res.name;
                cout<<"Sex"<<endl;cin>>res.sex;
                cout<<"Hometown"<<endl;cin>>res.home;
                switch(H.Insert(res,res.key_num)){
                    case Duplicate :
                        cout<<"Reload";break;
                    case Overflow  :
                        cout<<"Overflow";break;
                    case Success   :
                        cout<<"Succeed";		         }
                break;
            case 'D':
            case 'd':
                cout<<"Please input the num you want to delete:";
                cin>>x;
                switch(H.Remove(x)){
                    case Success   :
                        cout<<"Succeed"; break;
                    case NotPresent:
                        cout<<"NotPresent";
                }
                break;
            case 'F':
            case 'f':
                cout<<"Please input the num you want to find:";
                cin>>x;
                int pos;
                ResultCode result;
                result=H.Search(x,pos);
                switch(result){
                    case Success:
                        cout<<H.ht[pos].stu.key_num<<' '<<H.ht[pos].stu.name<<' '
                        <<H.ht[pos].stu.sex<<' '<<H.ht[pos].stu.home;
                        cout<<"Succeed";
                        break;
                    case NotPresent:cout<<"NotPresent";
                }
                break;
            case 'P':
            case 'p':
                for (int i = 0; i < m; ++i) {
                    cout<<i<<' ';
                    if(H.ht[i].key_!=-1) {
                        cout<<H.ht[i].stu.key_num<<' '<<H.ht[i].stu.name<<' '
                        <<H.ht[i].stu.sex<<' '<<H.ht[i].stu.home;
                    }
                    cout<<endl;
                }
        };
    }while(c!='X');
    return 0;
}